let config = require('../config');
let getKey = require("./rediskeyMaker.js");

let wrapper = require('co-redis');
let redis = require("redis");
let client

function initRedis(host, port, auth){
    let options = {auth_pass: auth}
    let cc = redis.createClient(port, host, options);
    cc.on("error", (err) => {
        console.log("Error " + err);
    });
    cc.config('SET', 'requirepass', config.redisAuth, (e)=>console.log(e))
    client = wrapper(cc);
}



/*** serviceRequest ***/

function setRequestGroups(domain, srid, gids) {
    if (gids) {
        let args = [getKey({domain, requests: {id: srid, groups: true}})].concat(gids)
        return client.sadd(args)
    }
    else {
        return deleteKey(getKey({domain, requests: {id: srid, groups: true}}));
    }
}

function getRequestGroups(domain, srid) {
    key = getKey({domain, requests: {id: srid, groups: true}})
    return client.smembers(key)
}

function addServiceRequest(domain, gid, srid, advance) {
    // 4102416000000为2100年时间戳,有优先权时排序值为正,越晚创建的越小
    args = [getKey({domain, group: {id: gid, queue: true}}), advance ? 4102416000000 - new Date().getTime() : -new Date().getTime(), srid]
    return client.zadd(args)
}

function deleteServiceRequest(domain, gid, srid) {
    let args = [getKey({domain, group: {id: gid, queue: true}}), srid]
    return client.zrem(args)
}

function removeServiceRequest(domain, gid) {
    return deleteKey(getKey({domain, group: {id: gid, queue: true}}))
}

function getServiceRequests(domain, gid) {
    let args = [getKey({domain, group: {id: gid, queue: true}}), '+inf', '-inf'];
    return client.zrevrangebyscore(args)
}



/*** group's agents ***/

function addAgentToGroup(domain, gid, agentid) {
    let args = [getKey({domain, group: {id: gid, agents: true}}), -new Date().getTime(), agentid]
    return client.zadd(args)
}

function deleteAgentFromGroup(domain, gid, agentid) {
    let args = [getKey({domain, group: {id: gid, agents: true}}), agentid];
    return client.zrem(args)
}

function getAgentsFromGroup(domain, gid) {
    let args = [getKey({domain, group: {id: gid, agents: true}}), '+inf', '-inf'];
    return client.zrevrangebyscore(args)
}



/*** agent's groups ***/

function addGroupsToAgent(domain, agentid, gids) {
    let args = [getKey({domain, agent: {id: agentid, groups: true}})];
    args = args.concat(gids);
    return client.sadd(args)
}

function deleteGroupFromAgent(domain, agentid, gid) {
    let args = [getKey({domain, agent: {id: agentid, groups: true}}), gid];
    return client.srem(args)
}

function removeAgentGroups(domain, agentid) {
    return deleteKey(getKey({domain, agent: {id: agentid, groups: true}}))
}

function getGroupFromAgent(domain, agentid) {
    let args = [getKey({domain, agent: {id: agentid, groups: true}})];
    return client.smembers(args)
}



/*** working agent ***/

function addWorkingAgent(domain, agentid) {
    let args = [getKey({domain, workingAgents: true}), agentid];
    return client.sadd(args)
}

function deleteWorkingAgent(domain, agentid) {
    let args = [getKey({domain, workingAgents: true}), agentid];
    return client.srem(args)
}


/*** domains ***/

function addDomain(domain) {
    let args = [getKey({domains: true}), domain];
    return client.sadd(args)
}



/*** agent status ***/

function setAgentOnline(domain, agentid, status) {
    let key = getKey({domain, agent: {id: agentid, online: true}})
    if (status) return client.set([key, 'y'])
    return client.del(key)
}

function checkAgentOnline(domain, agentid) {
    let args = [getKey({domain, agent: {id: agentid, online: true}})];
    return client.get(args)
}

function setAgentStatus(domain, agentid, status) {
    let key = getKey({domain, agent: {id: agentid, status: true}})
    if (status) return client.set([key, status])
    else return client.del(key)
}

function getAgentStatus(domain, agentid) {
    let args = [getKey({domain, agent: {id: agentid, status: true}})];
    return client.get(args)
}



/*** last distribute agent ***/

function setLastDistributeAgent(domain, gid, agentid) {
    let key = getKey({domain, group: {id: gid, lastDistributeAgent: true}})
    if (!agentid) return client.del(key)
    else return client.set(key, agentid)
}

function getLastDistributeAgent(domain, gid) {
    let args = [getKey({domain, group: {id: gid, lastDistributeAgent: true}})];
    return client.get(args)
}


/*** last distribute agent time ***/

function setLastDistributeTime(domain, agentid, time) {
    let key = getKey({domain, lastDistributeTime: {}})
    return client.hset(key, agentid, time)
}

function getLastDistributeTime(domain, agentids) {
    let key = getKey({domain, lastDistributeTime: {}})
    return client.hmget(key, agentids)
}



/*** key ***/

function deleteKey(k) {
    return client.del(k)
}

function getKeys(k) {
    return client.keys(k)
}



module.exports = {
    initRedis,

    setRequestGroups,
    getRequestGroups,
    addServiceRequest,
    deleteServiceRequest,
    removeServiceRequest,
    getServiceRequests,

    deleteAgentFromGroup,
    getAgentsFromGroup,
    addAgentToGroup,

    addGroupsToAgent,
    deleteGroupFromAgent,
    removeAgentGroups,
    getGroupFromAgent,

    addWorkingAgent,
    deleteWorkingAgent,

    addDomain,

    setAgentOnline,
    checkAgentOnline,
    setAgentStatus,
    getAgentStatus,

    setLastDistributeAgent,
    getLastDistributeAgent,

    setLastDistributeTime,
    getLastDistributeTime,

    deleteKey,
    getKeys,
}
